package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.DailysprintDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class DailysprintView {
    private InputText txtIncovenientes;
    private InputText txtCodigoSprint_Sprint;
    private InputText txtCodigoDaiSpr;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<DailysprintDTO> data;
    private DailysprintDTO selectedDailysprint;

    public DailysprintView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            DailysprintDTO dailysprintDTO = (DailysprintDTO) e.getObject();

            if (txtIncovenientes == null) {
                txtIncovenientes = new InputText();
            }

            txtIncovenientes.setValue(dailysprintDTO.getIncovenientes());

            if (txtCodigoSprint_Sprint == null) {
                txtCodigoSprint_Sprint = new InputText();
            }

            txtCodigoSprint_Sprint.setValue(dailysprintDTO.getCodigoSprint_Sprint());

            if (txtCodigoDaiSpr == null) {
                txtCodigoDaiSpr = new InputText();
            }

            txtCodigoDaiSpr.setValue(dailysprintDTO.getCodigoDaiSpr());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtIncovenientes != null) {
            txtIncovenientes.setValue(null);
            txtIncovenientes.setDisabled(true);
        }

        if (txtCodigoSprint_Sprint != null) {
            txtCodigoSprint_Sprint.setValue(null);
            txtCodigoSprint_Sprint.setDisabled(true);
        }

        if (txtCodigoDaiSpr != null) {
            txtCodigoDaiSpr.setValue(null);
            txtCodigoDaiSpr.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Dailysprint entity = null;

        try {
            String codigoDaiSpr = new String(txtCodigoDaiSpr.getValue()
                                                            .toString());
            entity = BusinessDelegatorView.getDailysprint(codigoDaiSpr);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtIncovenientes.setDisabled(false);
            txtCodigoSprint_Sprint.setDisabled(false);
            txtCodigoDaiSpr.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtIncovenientes.setValue(entity.getIncovenientes());
            txtIncovenientes.setDisabled(false);
            txtCodigoSprint_Sprint.setValue(entity.getSprint().getCodigoSprint());
            txtCodigoSprint_Sprint.setDisabled(false);
            txtCodigoDaiSpr.setValue(entity.getCodigoDaiSpr());
            txtCodigoDaiSpr.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveDailysprint(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtIncovenientes),
                FacesUtils.checkString(txtCodigoSprint_Sprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteDailysprint(FacesUtils.checkString(
                    txtCodigoDaiSpr));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateDailysprint(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtIncovenientes),
                FacesUtils.checkString(txtCodigoSprint_Sprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoDaiSpr == null) {
                txtCodigoDaiSpr = new InputText();
            }

            txtCodigoDaiSpr.setValue(selectedDailysprint.getCodigoDaiSpr());

            BusinessDelegatorView.deleteDailysprint(FacesUtils.checkString(
                    txtCodigoDaiSpr));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedDailysprint);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoDaiSpr,
        String incovenientes, String codigoSprint_Sprint)
        throws Exception {
        try {
            BusinessDelegatorView.updateDailysprint(codigoDaiSpr,
                incovenientes, codigoSprint_Sprint);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("DailysprintView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtIncovenientes() {
        return txtIncovenientes;
    }

    public void setTxtIncovenientes(InputText txtIncovenientes) {
        this.txtIncovenientes = txtIncovenientes;
    }

    public InputText getTxtCodigoSprint_Sprint() {
        return txtCodigoSprint_Sprint;
    }

    public void setTxtCodigoSprint_Sprint(InputText txtCodigoSprint_Sprint) {
        this.txtCodigoSprint_Sprint = txtCodigoSprint_Sprint;
    }

    public InputText getTxtCodigoDaiSpr() {
        return txtCodigoDaiSpr;
    }

    public void setTxtCodigoDaiSpr(InputText txtCodigoDaiSpr) {
        this.txtCodigoDaiSpr = txtCodigoDaiSpr;
    }

    public List<DailysprintDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataDailysprint();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<DailysprintDTO> dailysprintDTO) {
        this.data = dailysprintDTO;
    }

    public DailysprintDTO getSelectedDailysprint() {
        return selectedDailysprint;
    }

    public void setSelectedDailysprint(DailysprintDTO dailysprint) {
        this.selectedDailysprint = dailysprint;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
